/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.entities.Book;
import model.entities.Comment;
import model.entities.Folder;

/**
 *
 * @author HoangTien
 */
public class SqlBook {
    MyConnection mc = new MyConnection();
    Book book;
    public Book getBook(int id) { 
        Book book = null;
        List para = new ArrayList();
        SqlComment sqlComment = new SqlComment();
        para.add(id);
        try {
            ResultSet rs = mc.executeQuery("sp_get_book_by_bookId",para);
            if (rs.next()) {                
                List<Comment> listComment = sqlComment.getListComment(rs.getInt(1));
                book = new Book(rs.getInt(1), rs.getInt(2), rs.getString(3),rs.getString(4),rs.getString(5),listComment);
            }
        } catch (SQLException ex) {
            System.out.println("SqlBook::getBook::SQLException");
            ex.printStackTrace();
        }
        return book;
    }
    public List<Book> getListBooks(int cateId) {
        List<Book> data = new ArrayList<Book>();
        List para = new ArrayList();
        SqlComment sqlComment = new SqlComment();
        para.add(cateId);
        try {
            ResultSet rs = mc.executeQuery("sp_get_book_by_cateId",para);
            while (rs.next()) {                
                List<Comment> listComment = sqlComment.getListComment(rs.getInt(1));
                book = new Book(rs.getInt(1), rs.getInt(2), rs.getString(3),rs.getString(4),rs.getString(5),listComment);
                data.add(book);
            }
        } catch (SQLException ex) {
            System.out.println("SqlBook::getListBooks::SQLException");
            ex.printStackTrace();
        }
        return data;
    }
    public List<Book> getListBooks(String uname) {
        List<Book> data = new ArrayList<Book>();
        List para = new ArrayList();
        SqlComment sqlComment = new SqlComment();
        para.add(uname);
        try {
            ResultSet rs = mc.executeQuery("sp_get_book_by_uname",para);
            while (rs.next()) {                
                List<Comment> listComment = sqlComment.getListComment(rs.getInt(1));
                book = new Book(rs.getInt(1), rs.getInt(2), rs.getString(3),rs.getString(4),rs.getString(5),listComment);
                data.add(book);
            }
        } catch (SQLException ex) {
            System.out.println("SqlBook::getListBooks::SQLException");
            ex.printStackTrace();
        }
        return data;
    }
        
    public List<Book> getListBooks(Folder folder) {
        List<Book> data = new ArrayList<Book>();
        List para = new ArrayList();
        SqlComment sqlComment = new SqlComment();
        para.add(folder.getFolderId());
        try {
            ResultSet rs = mc.executeQuery("sp_get_book_by_folderId",para);
            while (rs.next()) {                
                List<Comment> listComment = sqlComment.getListComment(rs.getInt(1));
                book = new Book(rs.getInt(1), rs.getInt(2), rs.getString(3),
                        rs.getString(4),rs.getString(5),listComment);
                data.add(book); 
            }
        } catch (SQLException ex) {
            System.out.println("SqlBook::getListBooks::SQLException");
            ex.printStackTrace();
        }
        return data;
    }
}
